package com.gs.dao.impl;import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;

import com.gs.view.database.DatabasePanel;
public class BaseDaoImpl {
	private static  String driver;
	private static   String url ;
	private static   String user;
	private static   String password;
	Connection conn=null;
	static{
		init();
	}
	public static void init(){
		Properties properties=new Properties();
		String configFile=DatabasePanel.class.getPackage().getName() + ".resources.";
		configFile=configFile.replaceAll("\\.", "/");
		System.out.println("DatabasePanel>"+configFile);
		InputStream is=DatabasePanel.class.getResourceAsStream("resources/DatabasePanel.properties");
		try {
			properties.load(is);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(properties.getProperty("db.ip"));
		driver="com.mysql.jdbc.Driver";//properties.getProperty("dbDriver");
		//url="jdbc:mysql://"+localhost:3306/sys?zeroDateTimeBehavior=convertToNullproperties.getProperty("dbUrl");
		user=properties.getProperty("dbUsername");
		password=properties.getProperty("dbPwd");
	}
	public  Connection getConn() {
		try {
			Class.forName(driver);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		try {
			conn = DriverManager.getConnection(url, user, password);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}
	public void closeAll(Connection conn, Statement state,ResultSet rs) {
		try {
			if(rs!=null)
				rs.close();
			if(state!=null)
				state.close();
			if(conn!=null)
				conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public int execureUpdate(String sql,Object ...parms){
		Connection conn=null;
		PreparedStatement state=null;
		try {
			conn=getConn();
			state=conn.prepareStatement(sql);
			for(int i=0;i<parms.length;i++){
				if(parms[i] instanceof java.util.Date){
					java.util.Date ud=(Date) parms[i];
					java.sql.Date sd=new java.sql.Date(ud.getTime());
					parms[i]=sd;
				}
				state.setObject(i+1, parms[i]);
			}
			return state.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			closeAll(conn, state, null);
		}
		return 0;
	}
	public   <T>  T selectOne(String sql,Class<T> clazz,Object ...parms){
		Connection conn=null;
		PreparedStatement state=null;
		ResultSet rs=null;
		try {
			conn=getConn();
			state=conn.prepareStatement(sql);
			for(int i=0;i<parms.length;i++){
				if(parms[i] instanceof java.util.Date){
					java.util.Date ud=(Date) parms[i];
					java.sql.Date sd=new java.sql.Date(ud.getTime());
					parms[i]=sd;
				}
				state.setObject(i+1, parms[i]);
			}
			Method[] methods=clazz.getMethods();
			T obj=null;
			rs=state.executeQuery();
			ResultSetMetaData rsmd=rs.getMetaData();
			int num=rsmd.getColumnCount();
			if(rs.next()){
				obj=clazz.newInstance();
				for(int i=1;i<=num;i++){
					for(Method method:methods){
						String ColumnName="set"+rsmd.getColumnName(i);
						if(method.getName().equalsIgnoreCase(ColumnName)){
							method.invoke(obj, rs.getObject(rsmd.getColumnName(i)));
						}
					}
				}
				return (T)obj;
			}
			return null;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			closeAll(conn, state, rs);
		}
		return null;
	}
	public <T> List<T> selectList(String sql,Class<T> clazz,Object ...parms){
		Connection conn=null;
		PreparedStatement state=null;
		ResultSet rs=null;
		try {
			conn=getConn();
			state=conn.prepareStatement(sql);
			for(int i=0;i<parms.length;i++){
				if(parms[i] instanceof java.util.Date){
					java.util.Date ud=(Date) parms[i];
					java.sql.Date sd=new java.sql.Date(ud.getTime());
					parms[i]=sd;
				}
				state.setObject(i+1, parms[i]);
			}
			rs=state.executeQuery();
			List<T> list=new ArrayList<T>();
			Method ms[]=clazz.getMethods();
			while(rs.next()){
				ResultSetMetaData rsmd=rs.getMetaData();
				int coumnCount=rsmd.getColumnCount();
				T model=clazz.newInstance();
				for(int i=1;i<=coumnCount;i++){
					String columnLabel=rsmd.getColumnLabel(i);
					String methodName="set"+columnLabel;
					for(Method method:ms){
						if(method.getName().equalsIgnoreCase(methodName)){
							method.invoke(model, rs.getObject(columnLabel));
						}
					}
				}
				list.add(model);
			}
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			closeAll(conn, state, rs);
		}
		return null;
	}
}